<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
  if(checksubmit('submit')){
    if(empty($_GPC['class_id'])){
       message('班级不能为空','','error');
   }if (empty($_GPC['username'])) {
       message('账号不能为空','','error');
   }if(empty($_GPC['teacher_name'])){
       message('老师名称不能为空','','error');
   }if(empty($_GPC['password'])){
       message('密码不能为空','','error');
   }if ($_GPC['password'] != $_GPC['repassword']) {
       message('两次输入的密码不一致','','error');
   }if (!preg_match("/^[a-zA-Z]\w{5,19}$/",$_GPC['password'])) {
       message('密码为字母开头6-20位字母数字下划线','','error');
   }
    $list = pdo_getall('snx_teacher',array('uniacid'=>$_W['uniacid']),array('class_id'));
      $list = array_column($list, 'class_id');
      $list = implode(',', $list);
      // $id = implode(",", $_GPC['class_id']);
      //   for($i=0;$i<strlen($id);$i++){
      //       //$re[]=substr($id,$i,1);
      //       if (strstr($list,substr($id,$i,1))) {
      //         message('该班级已有老师存在','','error');
      //       }
      //   }
      foreach ($_GPC['class_id'] as $value) {
        if (strstr($list,$value)) {
           message('该班级已有老师存在','','error');
        }
      }
    $info = pdo_getall('snx_teacher',array('username'=>$_GPC['username'],'teacher_name'=>$_GPC['teacher_name']),array('class_id'));
    $teacherinfo = pdo_getall('snx_teacher',array('username'=>$_GPC['username']));
    if ($teacherinfo) {
      message('该账号已存在','','error');
    }
    $info = array_column($info,'class_id');
      if (empty($info)) {
         $class_id=implode(",", $_GPC['class_id']);
         $data['username']=$_GPC['username'];
         $data['c_id']=$_GPC['course_name'];
         $data['cou_id']=$_GPC['name'];
         $data['teacher_name']=$_GPC['teacher_name'];
         $data['password']=md5(md5($_GPC['password']));
         $data['state']=$_GPC['state'];
         $data['create_time']=date('Y-m-d H:i:s',time());
         $data['uniacid']=$_W['uniacid'];
         $data['school_id']=$_GPC['cate_name'];
         $data['class_id']=$class_id;
         $rst=pdo_insert('snx_teacher',$data);
         if($rst){
              message('操作成功！', $this->createWebUrl('teacher'), 'success');
          }else{
              message('操作失败！','','error');
         }   
    }else{
         $class_id=implode(",", $_GPC['class_id'])."," .$info[0];
         $test['username']=$_GPC['username'];
         $data['c_id']=$_GPC['course_name'];
         $data['cou_id']=$_GPC['name'];
         $test['teacher_name']=$_GPC['teacher_name'];
         $test['password']=md5(md5($_GPC['password']));
         $test['state']=$_GPC['state'];
         $test['create_time']=date('Y-m-d H:i:s',time());
         $test['uniacid']=$_W['uniacid'];
         $test['class_id']=$class_id;
         $rst=pdo_update('snx_teacher',$test,array('username'=>$_GPC["username"],'teacher_name'=>$_GPC['teacher_name']));
         if($rst){
              message('操作成功！', $this->createWebUrl('teacher'), 'success');
          }else{
              message('操作失败！','','error');
         }   
    }
} 


include $this->template('web/addteacher');